package boundary;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import dataview.Cibi;
import dataview.Data;
import dataview.FormatoReport;
import dataview.Menus;
import dataview.Ricette;
import dataview.TipoReportCibi;
import dataview.TipoReportMenu;
import dataview.TipoReportRicette;
import javax.swing.JTextField;
import calculator.GeneraReport;
import javax.swing.JButton;
import javax.swing.filechooser.FileFilter;
import com.lowagie.text.DocumentException;
import java.awt.Cursor;
import java.awt.Insets;
import java.awt.Dimension;
import java.io.File;
import java.util.Collection;
import javax.swing.ImageIcon;
import java.awt.Font;

public class SelezionaTipoReport extends JDialog {
	public static enum TipoDato {
		CIBI, RICETTE, MENU
	};
	
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private TipoDato tipo;
	private JLabel tipoL = null;
	private JComboBox tipoCB = null;
	private JLabel pathL = null;
	private JTextField pathTF = null;
	private JButton pathB = null;
	private JButton annullaB = null;
	private JButton esportaB = null;
	private JPanel buttonBar = null;
	private Home home;
	private Collection dati;
	
	private FormatoReport formato = FormatoReport.pdf;
	
	private String fileName;
	
	/**
	 * @param owner
	 */
	public SelezionaTipoReport(Home owner, TipoDato tipo, Collection dati) {
		super(owner);
		home = owner;
		this.dati = dati;
		this.tipo = tipo;
		initialize();
		setLocation((int)(owner.getLocationOnScreen().getX() + owner.getSize().getWidth() / 2 - getSize().getWidth() / 2), (int)(owner.getLocationOnScreen().getY() + owner.getSize().getHeight() / 2 - getSize().getHeight() / 2));
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(450, 160);
		this.setMinimumSize(new Dimension(450, 160));
		this.setPreferredSize(new Dimension(450, 160));
		this.setModal(true);
		this.setContentPane(getJContentPane());
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setTitle("Genera report " + tipo.toString().toLowerCase());
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.gridwidth = 4;
			gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints8.anchor = GridBagConstraints.EAST;
			gridBagConstraints8.insets = new Insets(0, 0, 0, 0);
			gridBagConstraints8.gridy = 2;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = -1;
			gridBagConstraints7.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints7.anchor = GridBagConstraints.EAST;
			gridBagConstraints7.gridy = -1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 2;
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridy = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 1;
			gridBagConstraints4.weightx = 0.0;
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints4.gridwidth = 1;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.gridx = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.gridy = 1;
			pathL = new JLabel();
			pathL.setText("Percorso");
			pathL.setFont(new Font("Dialog", Font.BOLD, 12));
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.gridwidth = 1;
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.gridx = 1;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.gridwidth = 1;
			gridBagConstraints11.anchor = GridBagConstraints.WEST;
			gridBagConstraints11.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints11.gridy = 0;
			tipoL = new JLabel();
			tipoL.setText("Tipo report");
			tipoL.setFont(new Font("Dialog", Font.BOLD, 12));
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridwidth = 1;
			gridBagConstraints1.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints.gridwidth = 1;
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.gridy = 0;
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(tipoL, gridBagConstraints11);
			jContentPane.add(getTipoCB(), gridBagConstraints2);
			jContentPane.add(pathL, gridBagConstraints3);
			jContentPane.add(getPathTF(), gridBagConstraints4);
			jContentPane.add(getPathB(), gridBagConstraints5);
			jContentPane.add(getButtonBar(), gridBagConstraints8);
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes tipoCB
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getTipoCB() {
		if (tipoCB == null) {
			if (tipo == TipoDato.CIBI){
				tipoCB = new JComboBox(TipoReportCibi.values());
				tipoCB.setFont(new Font("Dialog", Font.PLAIN, 12));
				tipoCB.setMinimumSize(new Dimension(100, 25));
				tipoCB.setPreferredSize(new Dimension(100, 25));
			}
			else if (tipo == TipoDato.RICETTE)
				tipoCB = new JComboBox(TipoReportRicette.values());
			else if (tipo == TipoDato.MENU) tipoCB = new JComboBox(TipoReportMenu.values());
		}
		return tipoCB;
	}
	
	/**
	 * This method initializes pathTF
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPathTF() {
		if (pathTF == null) {
			if (tipo == TipoDato.CIBI)
				fileName = "reportCibi_" + Data.toReportFormat();
			else if (tipo == TipoDato.RICETTE)
				fileName = "reportRicette_" + Data.toReportFormat();
			else if (tipo == TipoDato.MENU) fileName = "reportMenu_" + Data.toReportFormat();
			pathTF = new JTextField(new File(GeneraReport.REPORT_DIR + "/" + fileName + "." + formato.toString()).getAbsolutePath());
			pathTF.setEditable(false);
			pathTF.setColumns(0);
			pathTF.setFont(new Font("Dialog", Font.PLAIN, 12));
			pathTF.setPreferredSize(new Dimension(300, 20));
		}
		return pathTF;
	}
	
	/**
	 * This method initializes pathB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getPathB() {
		if (pathB == null) {
			pathB = new JButton();
			pathB.setText("");
			pathB.setCursor(new Cursor(Cursor.HAND_CURSOR));
			pathB.setIcon(new ImageIcon(getClass().getResource("/icone/search-icon.png")));
			pathB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
					JFileChooser fc = new JFileChooser(GeneraReport.REPORT_DIR);
					fc.setApproveButtonText("Conferma");
					fc.setSelectedFile(new File(GeneraReport.REPORT_DIR + "/" + fileName));
					fc.setAcceptAllFileFilterUsed(false);
					fc.addChoosableFileFilter(new MyFileFilter("Rich Text Format", "rtf"));
					fc.addChoosableFileFilter(new MyFileFilter("Text Files", "txt"));
					fc.addChoosableFileFilter(new MyFileFilter("HTML Files", "html"));
					fc.addChoosableFileFilter(new MyFileFilter("PDF Files", "pdf"));
					if (fc.showSaveDialog(jContentPane) == JFileChooser.APPROVE_OPTION) {
						formato = ((MyFileFilter)fc.getFileFilter()).getExt();
						pathTF.setText(fc.getSelectedFile().toString() + "." + formato);
					}
					setCursor(Cursor.getDefaultCursor());
				}
			});
		}
		return pathB;
	}
	
	/**
	 * This method initializes annullaB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAnnullaB() {
		if (annullaB == null) {
			annullaB = new JButton();
			annullaB.setText("Annulla");
			annullaB.setCursor(new Cursor(Cursor.HAND_CURSOR));
			annullaB.setFont(new Font("Dialog", Font.BOLD, 12));
			annullaB.setIcon(new ImageIcon(getClass().getResource("/icone/attachment-null-icon.png")));
			annullaB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					close();
				}
			});
		}
		return annullaB;
	}
	
	/**
	 * This method initializes esportaB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getEsportaB() {
		if (esportaB == null) {
			esportaB = new JButton();
			esportaB.setText("Crea report");
			esportaB.setCursor(new Cursor(Cursor.HAND_CURSOR));
			esportaB.setFont(new Font("Dialog", Font.BOLD, 12));
			esportaB.setIcon(new ImageIcon(getClass().getResource("/icone/attachment-confirm-icon.png")));
			esportaB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					switch (tipo) {
						case CIBI:
							try {
								home.gc.fareReport((Cibi)dati, (TipoReportCibi)tipoCB.getSelectedItem(), formato, pathTF.getText());
								JOptionPane.showMessageDialog(jContentPane, "Generazione report riuscita", "Operazione Terminata", JOptionPane.INFORMATION_MESSAGE);
								close();
							} catch (DocumentException e1) {
								JOptionPane.showMessageDialog(jContentPane, "Errore nella generazione report dei " + tipo.name().toLowerCase(), "Errore generazione report", JOptionPane.ERROR_MESSAGE);
							}
							break;
						case RICETTE:
							try {
								home.gr.fareReport((Ricette)dati, (TipoReportRicette)tipoCB.getSelectedItem(), formato, pathTF.getText());
								JOptionPane.showMessageDialog(jContentPane, "Generazione report riuscita", "Operazione Terminata", JOptionPane.INFORMATION_MESSAGE);
								close();
							} catch (DocumentException e1) {
								JOptionPane.showMessageDialog(jContentPane, "Errore nella generazione report dei " + tipo.name().toLowerCase(), "Errore generazione report", JOptionPane.ERROR_MESSAGE);
							}
							break;
						case MENU:
							try {
								home.gm.fareReport((Menus)dati, (TipoReportMenu)tipoCB.getSelectedItem(), formato, pathTF.getText());
								JOptionPane.showMessageDialog(jContentPane, "Generazione report riuscita", "Operazione Terminata", JOptionPane.INFORMATION_MESSAGE);
								close();
							} catch (DocumentException e1) {
								JOptionPane.showMessageDialog(jContentPane, "Errore nella generazione report dei " + tipo.name().toLowerCase(), "Errore generazione report", JOptionPane.ERROR_MESSAGE);
							}
							break;
					}
				}
			});
		}
		return esportaB;
	}
	
	/**
	 * This method initializes buttonBar
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonBar() {
		if (buttonBar == null) {
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 1;
			gridBagConstraints9.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints9.gridwidth = 1;
			gridBagConstraints9.gridy = 0;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = -1;
			gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints6.fill = GridBagConstraints.NONE;
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.gridy = -1;
			buttonBar = new JPanel();
			buttonBar.setLayout(new GridBagLayout());
			buttonBar.add(getAnnullaB(), gridBagConstraints6);
			buttonBar.add(getEsportaB(), gridBagConstraints9);
		}
		return buttonBar;
	}
	
	private void close() {
		this.dispose();
	}
	
	private class MyFileFilter extends FileFilter {
		private String descrizione;
		private String ext;
		
		public MyFileFilter(String descrizione, String ext) {
			super();
			this.descrizione = descrizione;
			this.ext = ext;
		}
		
		public FormatoReport getExt() {
			return FormatoReport.valueOf(ext);
		}
		
		@Override
		public boolean accept(File f) {
			return f.isDirectory() || f.getAbsolutePath().endsWith("." + ext);
		}
		
		@Override
		public String getDescription() {
			return descrizione + " (*." + ext + ")";
		}
	}
} //  @jve:decl-index=0:visual-constraint="0,0"
